package com.wannengbang.cloudleader.homepage;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gyf.immersionbar.ImmersionBar;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.AddressListBean;
import com.wannengbang.cloudleader.bean.BaseResponseBean;
import com.wannengbang.cloudleader.bean.ExchangeDetailsBean;
import com.wannengbang.cloudleader.bean.ExchangeListBean;
import com.wannengbang.cloudleader.bean.PrepareConfigBean;
import com.wannengbang.cloudleader.event.OrderRefushEvent;
import com.wannengbang.cloudleader.homepage.model.HomePageModelImpl;
import com.wannengbang.cloudleader.homepage.model.IHomePageModel;
import com.wannengbang.cloudleader.mine.SendDeliveryActivity;
import com.wannengbang.cloudleader.mine.model.IMineModel;
import com.wannengbang.cloudleader.mine.model.MineModelImpl;
import com.wannengbang.cloudleader.utils.DateTimeUtil;
import com.wannengbang.cloudleader.utils.ToastUtil;
import com.wannengbang.cloudleader.widget.AppTitleBar;
import com.wannengbang.cloudleader.widget.CommonNoTitleDialog;
import com.wannengbang.cloudleader.widget.ViewLoading;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ExchangeMyOrderDetailsActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_order_status)
    TextView tvOrderStatus;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_mobile)
    TextView tvMobile;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.ll_select_address)
    LinearLayout llSelectAddress;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.tv_order_no)
    TextView tvOrderNo;
    @BindView(R.id.tv_copy)
    TextView tvCopy;
    @BindView(R.id.tv_create_time)
    TextView tvCreateTime;
    @BindView(R.id.tv_delivery_type)
    TextView tvDeliveryType;
    @BindView(R.id.ll_delivery_type)
    LinearLayout llDeliveryType;
    @BindView(R.id.tv_delivery_no)
    TextView tvDeliveryNo;
    @BindView(R.id.tv_copy_no)
    TextView tvCopyNo;
    @BindView(R.id.ll_delivery_no)
    LinearLayout llDeliveryNo;
    @BindView(R.id.tv_machines_no)
    TextView tvMachinesNo;
    @BindView(R.id.ll_machines_no)
    LinearLayout llMachinesNo;
    @BindView(R.id.tv_btn_sign)
    TextView tvBtnSign;
    @BindView(R.id.tv_prepare_goods)
    TextView tvPrepareGoods;
    @BindView(R.id.tv_btn_delivery)
    TextView tvBtnDelivery;
    @BindView(R.id.tv_copy_machines)
    TextView tvCopyMachines;
    @BindView(R.id.ll_btn)
    LinearLayout llBtn;
    //    private int agent_id;
    private ExchangeListBean.DataBean.ItemListBean dataBean;
    private IHomePageModel homePageModel;
    private IMineModel mineModel;
    private CommonNoTitleDialog.Builder dialogBuild;
    private String shop_order_id;
    private String[] deliverName = {"顺丰速运", "圆通速递", "中通快递", "申通快递", "韵达速递", "邮政包裹", "德邦快递", "百世快递", "天天快递"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exchange_order_details);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarDarkFont(false)
                .navigationBarDarkIcon(true)
                .navigationBarColor(R.color.white)
                .statusBarDarkFont(false, 0.2f)
                .init();
        registerEventBus();
        initView();
        requestOrderShow();
    }

    @Subscribe
    public void updata(OrderRefushEvent event) {
        requestOrderShow();
    }

    public void initView() {
        homePageModel = new HomePageModelImpl();
        mineModel = new MineModelImpl();
        shop_order_id = getIntent().getStringExtra("shop_order_id");
//        UserInfoBean userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);
//        if (userInfoBean != null) {
//            agent_id = userInfoBean.getData().getId();
//        }
    }


    public void requestOrderShow() {
        ViewLoading.showProgress(mActivity);
        mineModel.requestExchangeOrderShow(shop_order_id, new DataCallBack<ExchangeDetailsBean>() {
            @Override
            public void onSuccessful(ExchangeDetailsBean bean) {
                dataBean = bean.getData();
                if (dataBean != null) {
                    AddressListBean.DataBean.ItemListBean addressBean = dataBean.getAgent_address();
                    tvName.setText(addressBean.getName());
                    tvMobile.setText(addressBean.getMobile());
                    tvAddress.setText(addressBean.getProvince() + addressBean.getCity() + addressBean.getArea() + addressBean.getAddress());
                    //1待发货 2已发货 3已收货
                    tvPrepareGoods.setVisibility(View.GONE);
                    tvBtnDelivery.setVisibility(View.GONE);
                    tvBtnSign.setVisibility(View.GONE);
                    llBtn.setVisibility(View.GONE);

                    if (dataBean.getStatus() == 1) {
                        tvOrderStatus.setText("等待(" + dataBean.getDeliver_name() + ")发货");
                        tvPrepareGoods.setVisibility(View.GONE);
                        tvBtnDelivery.setVisibility(View.GONE);
                    } else if (dataBean.getStatus() == 2) {
                        tvBtnSign.setVisibility(View.VISIBLE);
                        llBtn.setVisibility(View.VISIBLE);
                        tvOrderStatus.setText("待收货");
                    } else if (dataBean.getStatus() == 3) {
                        tvOrderStatus.setText("已完成");
                    }

                    tvCreateTime.setText(DateTimeUtil.format7(new Date(Long.parseLong(dataBean.getCreate_time()) * 1000)));
                    tvNumber.setText(dataBean.getNum());
                    tvOrderNo.setText(dataBean.getId());
                    StringBuffer buffer = new StringBuffer();
                    if (dataBean.getDevice_group() != null && dataBean.getDevice_group().size() > 0) {
                        for (PrepareConfigBean.DataBean bean1 : dataBean.getDevice_group()) {
                            buffer.append(bean1.getStart_no() + "-" + bean1.getEnd_no() + "\n");
                        }
                        tvMachinesNo.setText(buffer);
                        llMachinesNo.setVisibility(View.VISIBLE);
                    } else {
                        llMachinesNo.setVisibility(View.GONE);
                    }


                    if ((dataBean.getConsign_delivery_type() > 0 && dataBean.getConsign_delivery_type() < 10) && !TextUtils.isEmpty(dataBean.getConsign_delivery_no())) {
                        llDeliveryType.setVisibility(View.VISIBLE);
                        llDeliveryNo.setVisibility(View.VISIBLE);
                        tvDeliveryType.setText(deliverName[dataBean.getConsign_delivery_type() - 1]);
                        tvDeliveryNo.setText(dataBean.getConsign_delivery_no());
                    } else {
                        llDeliveryType.setVisibility(View.GONE);
                        llDeliveryNo.setVisibility(View.GONE);
                    }
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    public void requestStoreSignReceipt(String shop_order_id) {
        ViewLoading.showProgress(mActivity, "签收中......");
        homePageModel.requestStoreSignReceipt(shop_order_id, new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                EventBus.getDefault().post(new OrderRefushEvent());
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick({R.id.tv_copy_address, R.id.tv_copy, R.id.tv_copy_no, R.id.tv_btn_sign, R.id.tv_prepare_goods, R.id.tv_btn_delivery, R.id.tv_copy_machines})
    public void onViewClicked(View view) {
        if (dataBean == null) {
            return;
        }
        switch (view.getId()) {
            case R.id.tv_copy:
                //获取剪贴板管理器
                ClipboardManager cm = (ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
                // 创建普通字符型ClipData
                ClipData mClipData = ClipData.newPlainText("Label", dataBean.getId());
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);
                ToastUtil.showShort("复制成功");
                break;
            case R.id.tv_copy_no:
                //获取剪贴板管理器
                cm = (ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
                // 创建普通字符型ClipData
                mClipData = ClipData.newPlainText("Label", dataBean.getConsign_delivery_no());
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);
                ToastUtil.showShort("复制成功");
                break;
            case R.id.tv_copy_address:
                AddressListBean.DataBean.ItemListBean addressBean = dataBean.getAgent_address();
                if (addressBean == null) {
                    return;
                }
                String address = addressBean.getName() + " " + addressBean.getMobile() + " " + addressBean.getProvince() + addressBean.getCity() + addressBean.getArea() + addressBean.getAddress();
                //获取剪贴板管理器
                cm = (ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
                // 创建普通字符型ClipData
                mClipData = ClipData.newPlainText("Label", address);
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);
                ToastUtil.showShort("复制成功");
                break;
            case R.id.tv_copy_machines:
                //获取剪贴板管理器
                cm = (ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
                // 创建普通字符型ClipData
                mClipData = ClipData.newPlainText("Label", tvMachinesNo.getText().toString());
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);
                ToastUtil.showShort("复制成功");
                break;
            case R.id.tv_btn_sign:
                dialogBuild = new CommonNoTitleDialog.Builder(mActivity);
                dialogBuild.setMessage("确认收货?");
                dialogBuild.setPositiveButton("确定", v -> {
                    dialogBuild.dismiss();
                    requestStoreSignReceipt(dataBean.getId());
                });
                dialogBuild.setNegativeButton("取消", v -> dialogBuild.dismiss());
                dialogBuild.setCancelable(false);
                dialogBuild.show();
                break;
            case R.id.tv_prepare_goods:
                Intent intent = new Intent(mActivity, ExchangePrepareGoodsActivity.class);
                intent.putExtra("shop_order_id", dataBean.getId());
                startActivity(intent);
                break;
            case R.id.tv_btn_delivery:
                intent = new Intent(mActivity, SendDeliveryActivity.class);
                intent.putExtra("type", "exchange");
                intent.putExtra("shop_order_id", dataBean.getId());
                startActivity(intent);
                break;
        }
    }

}
